<?php
require_once 'config21.php';
if (!isset($_SESSION['userid'])) {
    header('Location: login21.php');
    exit;
}

// 添加教师
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['add_teacher'])) {
    $teacher_name = $_POST['teacher_name'];
    $gender = $_POST['gender'];
    $title = $_POST['title'];
    $contact_info = $_POST['contact_info'];
    $department_id = $_POST['department_id'];
    $age = $_POST['age'];
    $basic_salary = $_POST['basic_salary'];
    $bonus = $_POST['bonus'];
    
    $sql = "INSERT INTO teachers (teacher_name, gender, title, contact_info, department_id, age, basic_salary, bonus) 
            VALUES ('$teacher_name', '$gender', '$title', '$contact_info', '$department_id', '$age', '$basic_salary', '$bonus')";
    
    if ($conn->query($sql) === TRUE) {
        header('Location: teacher_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="zh-Hans">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>添加教师</title>
    <link rel="stylesheet" href="style.css">
    <style>
        .add-button {
            display: inline-block;
            background-color: #007bff;
            color: white;
            padding: 8px 16px;
            text-decoration: none;
            border-radius: 4px;
        }
        .add-button:hover {
            background-color: #0056d3;
        }
        form {
            max-width: 500px;
            margin: 20px auto;
        }
        label {
            display: inline-block;
            width: 100px;
            margin-bottom: 10px;
        }
        input[type="text"],
        input[type="number"],
        select {
            width: 200px;
            padding: 5px;
            margin-bottom: 10px;
        }
        input[type="submit"] {
            margin-top: 10px;
        }
    </style>
</head>
<body>
    <h2>添加教师</h2>
    <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
        <label for="teacher_name">教师姓名：</label>
        <input type="text" name="teacher_name" required><br>

        <label for="gender">性别：</label>
        <input type="radio" name="gender" value="男" required>男
        <input type="radio" name="gender" value="女" required>女<br>

        <label for="title">职称：</label>
        <input type="text" name="title" required><br>

        <label for="contact_info">联系方式：</label>
        <input type="text" name="contact_info" required><br>

        <label for="department_id">所属系部：</label>
        <select name="department_id" required>
            <?php
            $dept_sql = "SELECT * FROM departments";
            $dept_result = $conn->query($dept_sql);
            while ($dept_row = $dept_result->fetch_assoc()) {
                echo "<option value='" . $dept_row['department_id'] . "'>" . $dept_row['department_name'] . "</option>";
            }
            ?>
        </select><br>

        <label for="age">年龄：</label>
        <input type="number" name="age" min="18" max="100" required><br>

        <label for="basic_salary">基本工资：</label>
        <input type="number" name="basic_salary" step="0.01" min="0" required><br>

        <label for="bonus">奖金：</label>
        <input type="number" name="bonus" step="0.01" min="0" required><br>

        <input type="submit" value="添加" name="add_teacher">
    </form>
    <div align="right"><a href="teacher_management21.php" class="add-button">返回</a></div>
</body>
</html>